package com.document.mail;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.regex.Pattern;

import javax.mail.MessagingException;

import lostsky.tool.mail.MailSender;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.document.MyActivity;
import com.document.R;

public class SentMail extends MyActivity {
	private EditText usename;//邮件用户名
	private EditText password;//邮件密码
	private EditText tomail;//发送地址
	private EditText theme;//主题
	private EditText content;//内容
	private ImageButton cancel;//取消附件
	private Button send;//发送邮件
	private TextView attachment;//附件显示
	private String use=null;
	private String psd=null;
	private String adress;
	private String Theme;
	private String Content;
	String smtpHost;//SMTP主机地址
	int contentType=MailSender.CONTENT_TYPE_HTML;//邮件正文类型 两种: html格式和纯文本格式
	String filepath;
	ProgressDialog progressdialog;
	String contentEncoding=null;//邮件正文编码 默认为null
	String fileNameEncoding=null;//附件文件名编码 默认为null
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.mail_sent);
		Intent i=getIntent();
        Uri uri=(Uri)i.getData();
        filepath=uri.getPath(); 
        File f=new File(filepath);
        String fn=f.getName();
        usename=(EditText)findViewById(R.id.usename1);
		password=(EditText)findViewById(R.id.password1);
		tomail=(EditText)findViewById(R.id.tomail);
		theme=(EditText)findViewById(R.id.theme1);
		attachment=(TextView)findViewById(R.id.attachment);
		content=(EditText)findViewById(R.id.content1);	
		send=(Button)findViewById(R.id.send1);
		attachment.setText("附件:  "+fn);
         System.out.println("filepath"+filepath);
         SharedPreferences preference = getSharedPreferences("person",Context.MODE_PRIVATE);

         usename.setText(preference.getString("User",""));          
         password.setText(preference.getString("Psw", ""));
         tomail.setText(preference.getString("adress", ""));
 	
      

         send.setOnClickListener(new OnClickListener() {
		
			public void onClick(View v) {
				
		        usename = (EditText) findViewById(R.id.usename1);        //编辑框对象
		        password = (EditText) findViewById(R.id.password1);
		         use = usename.getText().toString();              
		         psd = password.getText().toString();		        
		  		adress=tomail.getText().toString();
		  		Theme=theme.getText().toString();
		  		Content=content.getText().toString();	
		  		System.out.println("---------------psd--------------  "+psd);
		        if((!use.equals(""))&&(!psd.equals(""))){
		        	 //使用正则 得到smtpHost值
					 String regex="@";	    						 
						Pattern p=Pattern.compile(regex);	    								
						String[] r=p.split(use);    									    									
						smtpHost=r[1];
						System.out.print("---------------smtpHost--------------  "+smtpHost);
					// 保存用户名与密码
			        SharedPreferences preference = getSharedPreferences("person",Context.MODE_PRIVATE);
			        Editor edit = preference.edit();
			        edit.putString("User",use);      
			        edit.putString("Psw",psd);
			        edit.putString("adress",adress);
			        edit.commit();
				progressdialog = ProgressDialog.show(SentMail.this, "正在发送...", "请稍后...", true, false);
				progressdialog.setCancelable(true);
                new Thread( new Runnable() {
					public void run() {
						
						sendmain();     
		                             
		                          
		                        //向handler发消息  
		                        handler.sendEmptyMessage(0);    
		                    }  				
				}
                ).start(); 
				
				
			}
		        else{
					Toast.makeText(SentMail.this, "请输入邮箱账户信息", Toast.LENGTH_LONG).show();
				}
		        }
			
		});
     
		}
	 // 用Handler来更新UI 实现文件删除
    Handler handler = new Handler(){  
 
       @Override  
       public void handleMessage(Message msg) {  
             
           //关闭ProgressDialog  
           progressdialog.dismiss();                         
           Toast.makeText(getApplicationContext(), "发送成功...", Toast.LENGTH_LONG).show();
          
           
       }};  
	public void sendmain(){
		//附件
		File[] attaches=new File[1];
		attaches[0]=new File(filepath);

		
		try {
			MailSender sender=new MailSender("smtp."+smtpHost, use, psd, 
					use,adress, Theme, 
					Content, contentEncoding, contentType, 
					attaches, fileNameEncoding);
			/*发送*/
			sender.send();
			
			
		} catch (MessagingException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	
	}
}
